<?php
/**
 * @link https://gitee.com/toshcn/micro-hau
 * @copyright Copyright (c) 2022/4/21 micro-hau
 * @author toshcn <toshcn@foxmail.com>
 */

namespace common\models;

use yii\base\Model;

/**
 * 公共模型基类
 * Class BaseModel
 * @package common\models
 */
class BaseModel extends Model
{
    /**
     * 获取表单第一个校验错误信息
     * @return string
     */
    public function getFirstErrorMessage()
    {
        $msg = \Yii::t('common', 'Field Error.');
        if ($this->getErrors()) {
            foreach ($this->getErrors() as $name => $error) {
                return $msg = $error[0];
            }
        }
        return $msg;
    }

    /**
     * 获取表单第一个校验错误字段
     * @return string|boolean
     */
    public function getFirstErrorField()
    {
        if ($this->getErrors()) {
            foreach ($this->getErrors() as $name => $error) {
                return $name;
            }
        }
        return false;
    }

    /**
     * 获取表单第一个校验错误的第一个错误信息
     * @return array|boolean
     */
    public function getFirstOneError()
    {
        if ($this->getErrors()) {
            foreach ($this->getErrors() as $name => $error) {
                return [$name => $error[0]];
            }
        }
        return false;
    }
}